/*
 *    Copyright (c) 2018-2025, excesysiot All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: excesysiot
 */
package com.excesys.exsecs.access.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.excesys.exsecs.access.entity.DataFlowOperation;
import com.excesys.exsecs.access.mapper.DataFlowOperationMapper;
import com.excesys.exsecs.access.mapper.ExecuteSqlMapper;
import com.excesys.exsecs.access.service.DataFlowOperationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据源表
 *
 * @author excesysiot
 * @date 2019-03-31 16:00:20
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DataFlowOperationServiceImpl implements DataFlowOperationService {

	private final ExecuteSqlMapper executeSqlMapper;
	private final DataFlowOperationMapper dataFlowOperationMapper;

	@Override
	@DS("secord")
	public int checkTableExists(String database, String tableName) {
		String sql = "SELECT COUNT(1) FROM information_schema.tables \n" +
				"  WHERE table_schema = '"+ database +"' AND table_name = '"+ tableName +"'";
		return executeSqlMapper.checkTableExists(sql);
	}

	@Override
	@DS("secord")
	public void createCustomTable(String tableName) {
		String sql = "CREATE TABLE "+ tableName +" ( \n" +
				"  `unit_no` varchar(32) COLLATE utf8mb4_general_ci NOT NULL COMMENT '设备id',\n" +
				"  `data` json DEFAULT NULL COMMENT '数据json',\n" +
				"  `create_time` datetime NOT NULL COMMENT '上报时间',\n" +
				"  INDEX idx_create_time (create_time)\n" +
				") ENGINE=InnoDB;";
		executeSqlMapper.executeCreateTableSql(sql);
	}

	@Override
	@DS("secord")
	public List<Map> queryFieldValue(String tableName, String field, String startTime, String endTime, int limit) {
		String sql = "SELECT JSON_EXTRACT( DATA, '$." + field + "' ) AS value , create_time AS createTime FROM " + tableName + " \n" +
				"  WHERE create_time BETWEEN '"+ startTime +"' AND '"+ endTime+"' LIMIT " + limit;
		return executeSqlMapper.executeQuerySql(sql);
	}

	@Override
	@DS("secord")
	public void executeInsertSql(DataFlowOperation dataFlow) {
		dataFlowOperationMapper.insertDataFlow(dataFlow);
	}

	@Override
	@DS("secord")
	public void dropCustomTable(String tableName) {
		String sql = "DROP TABLE IF EXISTS "+ tableName;
		executeSqlMapper.executeDropTableSql(sql);
	}
}
